namespace ASPNET.Queries
{
    using System;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Diagnostics;
    using System.Runtime.CompilerServices;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Microsoft.VisualBasic.CompilerServices;
    using System.Web;
    using System.Text;
    using ASBCC.Business;
    using ASB.CC.Business.ACR;

    public class ACR : UserControl
    {
        protected System.Web.UI.WebControls.ImageButton btnPageFirst;
        protected System.Web.UI.WebControls.ImageButton btnPagePrev;
        protected System.Web.UI.WebControls.ImageButton btnPageNext;
        protected System.Web.UI.WebControls.ImageButton btnPageLast;
        protected System.Web.UI.WebControls.Label lblRecordCount;
        protected System.Web.UI.WebControls.Label lblPageCount;
        protected System.Web.UI.WebControls.Label lblCurrentPageIndex;
        protected System.Web.UI.WebControls.ImageButton btnGotoPage;
        protected System.Web.UI.WebControls.TextBox txtPage;

        [AccessedThroughProperty("BankdetailGrd")]
        private DataGrid _BankdetailGrd;
        [AccessedThroughProperty("btnClear")]
        private Button _btnClear;
        [AccessedThroughProperty("Btnselect")]
        private Button _Btnselect;
        [AccessedThroughProperty("btnExport")]
        private Button _btnExport;
        [AccessedThroughProperty("DataGrid1")]
        private DataGrid _DataGrid1;
        [AccessedThroughProperty("DropDownList1")]
        private DropDownList _DropDownList1;
        [AccessedThroughProperty("Textbusinessname")]
        private TextBox _Textbusinessname;
        [AccessedThroughProperty("TextChinesename")]
        private TextBox _TextChinesename;
        [AccessedThroughProperty("Textcity")]
        private TextBox _Textcity;
        [AccessedThroughProperty("Textcust")]
        private TextBox _Textcust;
        [AccessedThroughProperty("Textname")]
        private TextBox _Textname;

        public ACR()
        {
            base.Init += new EventHandler(this.Page_Init);
            base.Load += new EventHandler(this.Page_Load);
        }

        public void ApplyFilter_Click(object Sender, EventArgs E)
        {
            this.BindDataGrid1(1);
            //string CommandText;
            //this.DataGrid1.CurrentPageIndex = 0;
            //string ConnectionString = ConfigurationSettings.AppSettings["ConnectionString"];
            //int a =0;
            //string custfilterValue="";
            //if(this.Textcust.Text!="" && !int.TryParse(this.Textcust.Text,out a))
            //    custfilterValue="0";
            //else
            //    custfilterValue = this.Textcust.Text.Replace("'", "''");
            //string chinesenamefilterValue = this.TextChinesename.Text.Replace("'", "''");
            //string namefilterValue = this.Textname.Text.Replace("'", "''");
            //string cityfilterValue = this.Textcity.Text.Replace("'", "''");
            //string ProvincefilterValue = this.DropDownList1.SelectedItem.Text.Replace("'", "''");
            //string businessnamefilterValue = this.Textbusinessname.Text.Replace("'", "''");
            //if (StringType.StrCmp(custfilterValue, "", false) != 0)
            //{
            //    CommandText = "select acrcode, legal_name,Business_Name,CHname,city,Provincename from ACR_SAP where acrcode = '" + custfilterValue + "'";
            //}
            //else
            //{
            //    CommandText = "select acrcode, legal_name,Business_Name,CHname,city,Provincename from ACR_SAP where acrcode like '%'";
            //}
            //if (StringType.StrCmp(namefilterValue, "", false) != 0)
            //{
            //    CommandText = CommandText + " and Legal_name like '%" + namefilterValue + "%' ";
            //}
            //if (StringType.StrCmp(chinesenamefilterValue, "", false) != 0)
            //{
            //    CommandText = CommandText + " and CHname like '%" + chinesenamefilterValue + "%' ";
            //}
            //if (StringType.StrCmp(cityfilterValue, "", false) != 0)
            //{
            //    CommandText = CommandText + " and city like '%" + cityfilterValue + "%' ";
            //}
            //if (StringType.StrCmp(ProvincefilterValue, "-- Province--", false) != 0)
            //{
            //    CommandText = CommandText + "  and Provincename like '%" + ProvincefilterValue + "%' ";
            //}
            //if (StringType.StrCmp(businessnamefilterValue, "", false) != 0)
            //{
            //    CommandText = CommandText + " and Business_Name like '%" + businessnamefilterValue + "%'";
            //}
            //CommandText = CommandText + "order by acrcode desc";
            //SqlConnection myConnection = new SqlConnection(ConnectionString);
            //SqlDataAdapter myCommand = new SqlDataAdapter(CommandText, myConnection);
            //DataSet ds = new DataSet();
            //myCommand.Fill(ds);
            //this.DataGrid1.DataSource = ds;
            //this.DataGrid1.DataBind();
        }


        protected void btnExport_Click(object sender, EventArgs e)
        {
            //HttpContext.Current.Response.Charset = "UTF-8";
            //HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
            //HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("MyCompany.xls", Encoding.UTF8).ToString());
            //HttpContext.Current.Response.ContentType = "application/ms-excel";
            this.DataGrid1.AllowPaging = false;
            this.DataGrid1.AllowSorting = false;
            BindDataGrid1(1);
            BEGDC.CC.Web.Control.ExportDataGridToExcel.Export(this.Page, this.DataGrid1, "ACRList.xls");
            //System.IO.StringWriter tw = new System.IO.StringWriter();
            //HtmlTextWriter hw = new HtmlTextWriter(tw);
            //this.DataGrid1.RenderControl(hw);
            //HttpContext.Current.Response.Write(tw.ToString());
            //HttpContext.Current.Response.End();
            this.DataGrid1.AllowPaging = true;
            this.DataGrid1.AllowSorting = true;
            this.DataGrid1.DataBind();
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            this.DataGrid1.SelectedIndex = 0;
            this.DropDownList1.SelectedIndex = 0;
            this.Textbusinessname.Text = "";
            this.TextChinesename.Text = "";
            this.Textcity.Text = "";
            this.Textcust.Text = "";
            this.Textname.Text = "";
            this.BindDataGrid1(1);
        }

        

        public void BindDataGrid1(int pageindex)
        {
            string CommandText;
            //this.DataGrid1.CurrentPageIndex = 0;
            string ConnectionString = ConfigurationSettings.AppSettings["ConnectionString"];
            int a = 0;
            this.Textcust.Text = PageSafeHelper.SafeText(this.Textcust.Text);
            this.TextChinesename.Text = PageSafeHelper.SafeText(this.TextChinesename.Text);
            this.Textname.Text = PageSafeHelper.SafeText(this.Textname.Text);
            this.Textcity.Text = PageSafeHelper.SafeText(this.Textcity.Text);
            this.Textbusinessname.Text = PageSafeHelper.SafeText(this.Textbusinessname.Text);
            string custfilterValue = "";
            if (this.Textcust.Text != "" && !int.TryParse(this.Textcust.Text, out a))
                custfilterValue = "0";
            else
                custfilterValue = this.Textcust.Text;
            string chinesenamefilterValue = this.TextChinesename.Text;
            string namefilterValue = this.Textname.Text;
            string cityfilterValue = this.Textcity.Text;
            string ProvincefilterValue = this.DropDownList1.SelectedItem.Text;
            string businessnamefilterValue = this.Textbusinessname.Text;
            if (StringType.StrCmp(custfilterValue, "", false) != 0)
            {
                CommandText = "select acrcode, legal_name,Business_Name,CHname,city,Provincename from ACR_SAP where acrcode = '" + custfilterValue + "'";
            }
            else
            {
                CommandText = "select acrcode, legal_name,Business_Name,CHname,city,Provincename from ACR_SAP where acrcode like '%'";
            }
            if (StringType.StrCmp(namefilterValue, "", false) != 0)
            {
                CommandText = CommandText + " and Legal_name like '%" + namefilterValue + "%' ";
            }
            if (StringType.StrCmp(chinesenamefilterValue, "", false) != 0)
            {
                CommandText = CommandText + " and CHname like '%" + chinesenamefilterValue + "%' ";
            }
            if (StringType.StrCmp(cityfilterValue, "", false) != 0)
            {
                CommandText = CommandText + " and city like '%" + cityfilterValue + "%' ";
            }
            if (StringType.StrCmp(ProvincefilterValue, "-- Province--", false) != 0)
            {
                CommandText = CommandText + "  and Provincename like '%" + ProvincefilterValue + "%' ";
            }
            if (StringType.StrCmp(businessnamefilterValue, "", false) != 0)
            {
                CommandText = CommandText + " and Business_Name like '%" + businessnamefilterValue + "%'";
            }
            CommandText = CommandText + "order by acrcode desc";
            SqlConnection myConnection = new SqlConnection(ConnectionString);
            SqlDataAdapter myCommand = new SqlDataAdapter(CommandText, myConnection);
            DataSet ds = new DataSet();
            myCommand.Fill(ds);
            this.DataGrid1.DataSource = ds;
            this.DataGrid1.DataBind();
            SetPageCount(ds.Tables[0].Rows.Count, pageindex);
        }

        public void BindDetailGrid()
        {
            if (this.DataGrid1.SelectedIndex != -1)
            {
                string ConnectionString = ConfigurationSettings.AppSettings["ConnectionString"];
                string filterValue = StringType.FromObject(this.DataGrid1.DataKeys[this.DataGrid1.SelectedIndex]).Replace("'", "''");
                string CommandText = "select ACRCODE,客户分类, 银行码, 银行帐户,纳税人登记号,贸易伙伴,地址 from BankDetails where ACRCODE = '" + filterValue + "'";
                SqlConnection myConnection = new SqlConnection(ConnectionString);
                SqlCommand myCommand = new SqlCommand(CommandText, myConnection);
                myConnection.Open();
                this.BankdetailGrd.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            }
            this.BankdetailGrd.DataBind();
        }

        public void DataGrid_Page(object Sender, DataGridPageChangedEventArgs E)
        {
            if (this.DataGrid1.SelectedIndex != -1)
            {
                this.DataGrid1.SelectedIndex = -1;
                this.BindDetailGrid();
            }
            this.DataGrid1.CurrentPageIndex = E.NewPageIndex;
            this.BindDataGrid1(1);
        }

        public void DataGrid_Select(object Sender, EventArgs E)
        {
            this.BindDetailGrid();
        }

        [DebuggerStepThrough]
        private void InitializeComponent()
        {
            this.btnPageFirst.Click += new System.Web.UI.ImageClickEventHandler(this.btnPageFirst_Click);
            this.btnPagePrev.Click += new System.Web.UI.ImageClickEventHandler(this.btnPagePrev_Click);
            this.btnPageNext.Click += new System.Web.UI.ImageClickEventHandler(this.btnPageNext_Click);
            this.btnPageLast.Click += new System.Web.UI.ImageClickEventHandler(this.btnPageLast_Click);
            this.btnGotoPage.Click += new System.Web.UI.ImageClickEventHandler(this.btnGotoPage_Click);
        }

        private void Page_Init(object sender, EventArgs e)
        {
            if (!CommonHelper.IsACRAdmin((this.Page.User as CSPrincipal).Role2s))
                this.btnExport.Visible = false;
            else
                this.btnExport.Visible = true;
            this.InitializeComponent();
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                string ConnectionString = ConfigurationSettings.AppSettings["ConnectionString"];
                string CommandText = "select distinct Provincename from ACR_SAP where  Provincename IS NOT NULL order by Provincename";
                SqlConnection myConnection = new SqlConnection(ConnectionString);
                SqlCommand myCommand = new SqlCommand(CommandText, myConnection);
                this.DropDownList1.DataTextField = "Provincename";
                myConnection.Open();
                this.DropDownList1.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
                this.DropDownList1.DataBind();
                this.DropDownList1.Items.Insert(0, "-- Province--");
                ApplyFilter_Click(null, null);
            }
        }

        protected virtual Button btnExport
        {
            get
            {
                return this._btnExport;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._btnExport != null)
                {
                    this._btnExport.Click -= new EventHandler(this.btnExport_Click);
                }
                this._btnExport = value;
                if (this._btnExport != null)
                {
                    this._btnExport.Click += new EventHandler(this.btnExport_Click);
                }
            }
        }

        protected virtual DataGrid BankdetailGrd
        {
            get
            {
                return this._BankdetailGrd;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._BankdetailGrd != null)
                {
                }
                this._BankdetailGrd = value;
                if (this._BankdetailGrd != null)
                {
                }
            }
        }

        protected virtual Button Btnselect
        {
            get
            {
                return this._Btnselect;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._Btnselect != null)
                {
                }
                this._Btnselect = value;
                if (this._Btnselect != null)
                {
                }
            }
        }

        protected virtual DataGrid DataGrid1
        {
            get
            {
                return this._DataGrid1;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._DataGrid1 != null)
                {
                }
                this._DataGrid1 = value;
                if (this._DataGrid1 != null)
                {
                }
            }
        }

        protected virtual DropDownList DropDownList1
        {
            get
            {
                return this._DropDownList1;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._DropDownList1 != null)
                {
                }
                this._DropDownList1 = value;
                if (this._DropDownList1 != null)
                {
                }
            }
        }

        protected virtual TextBox Textbusinessname
        {
            get
            {
                return this._Textbusinessname;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._Textbusinessname != null)
                {
                }
                this._Textbusinessname = value;
                if (this._Textbusinessname != null)
                {
                }
            }
        }

        protected virtual TextBox TextChinesename
        {
            get
            {
                return this._TextChinesename;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._TextChinesename != null)
                {
                }
                this._TextChinesename = value;
                if (this._TextChinesename != null)
                {
                }
            }
        }

        protected virtual TextBox Textcity
        {
            get
            {
                return this._Textcity;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._Textcity != null)
                {
                }
                this._Textcity = value;
                if (this._Textcity != null)
                {
                }
            }
        }

        protected virtual TextBox Textcust
        {
            get
            {
                return this._Textcust;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._Textcust != null)
                {
                }
                this._Textcust = value;
                if (this._Textcust != null)
                {
                }
            }
        }

        protected virtual TextBox Textname
        {
            get
            {
                return this._Textname;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._Textname != null)
                {
                }
                this._Textname = value;
                if (this._Textname != null)
                {
                }
            }
        }

        #region page navigation

        //public System.Security.Principal.IPrincipal User
        //{
        //    get
        //    {                
        //            return this.Page.User;
        //    }
        //    set
        //    {
        //        ViewState["User"] = value;
        //    }
        //}

        public int ProjectID
        {
            get
            {
                if (ViewState["ProjectID"] != null) return (int)ViewState["ProjectID"];
                else
                    return 0;
            }
            set
            {
                ViewState["ProjectID"] = value;
            }
        }

        private int _PageCount
        {
            get
            {
                if (ViewState["PageCount"] != null) return (int)ViewState["PageCount"];
                else
                    return 0;
            }
            set
            {
                ViewState["PageCount"] = value;
            }
        }

        private int _CurrentPageIndex
        {
            get
            {
                if (ViewState["CurrentPageIndex"] != null) return (int)ViewState["CurrentPageIndex"];
                else
                    return 0;
            }
            set
            {
                ViewState["CurrentPageIndex"] = value;
            }
        }

        private int _RowCount
        {
            get
            {
                if (ViewState["RowCount"] != null) return (int)ViewState["RowCount"];
                else
                    return 0;
            }
            set
            {
                ViewState["RowCount"] = value;
            }
        }

        private void SetPageNavi(int newPage)
        {
            if (newPage <= 1)
            {

                this.btnPageFirst.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path + "/images/pg_first_inact.gif";
                this.btnPagePrev.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path + "/images/pg_pre_inact.gif";
                this.btnPageFirst.Attributes["disabled"] = "true";
                this.btnPagePrev.Attributes["disabled"] = "true";
            }
            else
            {
                this.btnPageFirst.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path + "/images/pg_first.gif";
                this.btnPagePrev.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path + "/images/pg_pre.gif";
                this.btnPageFirst.Attributes.Remove("disabled");
                this.btnPagePrev.Attributes.Remove("disabled");
            }

            if (newPage >= _PageCount)
            {
                this.btnPageNext.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path + "/images/pg_next_inact.gif";
                this.btnPageLast.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path + "/images/pg_last_inact.gif";
                this.btnPageNext.Attributes["disabled"] = "true";
                this.btnPageLast.Attributes["disabled"] = "true";
            }
            else
            {
                this.btnPageNext.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path + "/images/pg_next.gif";
                this.btnPageLast.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path + "/images/pg_last.gif";
                this.btnPageNext.Attributes.Remove("disabled");
                this.btnPageLast.Attributes.Remove("disabled");
            }

            this.lblCurrentPageIndex.Text = newPage.ToString();
            this.lblPageCount.Text = _PageCount.ToString();
            this.lblRecordCount.Text = _RowCount.ToString();
        }

        private void SetPageCount(int count, int pageIndex)
        {
            if (count > 0)
            {
                _RowCount = count;
                _PageCount = count / this.DataGrid1.PageSize;
                if (count % this.DataGrid1.PageSize > 0) _PageCount++;

                _CurrentPageIndex = pageIndex;
                SetPageNavi(pageIndex);
            }
            else
            {
                _RowCount = count;
                _PageCount = count;
                _CurrentPageIndex = 0;
                SetPageNavi(0);
            }
        }


        #endregion

        #region ---------- paging events ----------------

        private void btnPageFirst_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            this.DataGrid1.CurrentPageIndex = 0;
            this.BindDataGrid1(1);
        }

        private void btnPagePrev_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            if (_CurrentPageIndex == 1) return;
            this.DataGrid1.CurrentPageIndex = _CurrentPageIndex - 2;
            this.BindDataGrid1(_CurrentPageIndex - 1);
        }

        private void btnPageNext_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            if (_CurrentPageIndex == _PageCount) return;
            this.DataGrid1.CurrentPageIndex = _CurrentPageIndex;
            this.BindDataGrid1(_CurrentPageIndex + 1);
        }

        private void btnPageLast_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            this.DataGrid1.CurrentPageIndex = _PageCount - 1;
            this.BindDataGrid1(_PageCount);
        }

        private void btnGotoPage_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            if (this.txtPage.Text == string.Empty)
            {
                this.txtPage.Text = "1";
            }
            int gotopage = 1;
            try
            {
                gotopage = Convert.ToInt32(this.txtPage.Text);
            }
            catch { }
            int currentpage = 0;

            if (gotopage == 0) return;
            if (_PageCount == 0) return;

            if (gotopage >= _PageCount)
            {
                gotopage = _PageCount;
                currentpage = gotopage - 1;
                if (currentpage < 0) currentpage = 0;
            }
            else if (gotopage <= 0)
            {
                gotopage = 1;
            }
            else
            {
                currentpage = gotopage - 1;
            }

            this.DataGrid1.CurrentPageIndex = currentpage;

            this.BindDataGrid1(gotopage);

            this.txtPage.Text = string.Empty;
        }

        #endregion
    }
}

